<template>
  <div class="dialog">
    <el-dialog width="45%" title="审核权限" :visible="true" :close-on-click-modal="false" :before-close="close">
      <el-form ref="form" :model="form" label-width="110px">
        <el-form-item label="产品审核：">
          <el-row>
            <el-tree
              ref="treeNode"
              show-checkbox
              default-expand-all
              :data="treeList"
              node-key="industryCode"
              :props="props"
              :default-checked-keys="defaultKeys"
              clearable
              style="height:370px;overflow-y:auto;"
              @check-change="handleCheckChange"
            />
          </el-row>
        </el-form-item>
      </el-form>
      <div slot="footer">
        <el-button @click="close">取消</el-button>
        <el-button type="primary" :loading="loading" @click="confirm">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getIndustryList } from '@/api/dictionary'
import { addUserApprove, getUserDetail } from '@/api/system'
export default {
  name: 'Audit',
  props: {
    id: {
      type: [String, Number],
      default: ''
    }
  },

  data() {
    return {
      defaultKeys: [], // 默认选中权限
      treeList: [],
      props: {
        label: 'industryName',
        children: 'childMenuEntities'
      },
      loading: false,
      form: {
        industryCodes: []
      }
    }
  },

  computed: {
  },

  created() {
    if (this.id) {
      this.getDetail()
    }
    this.getTreeListHandle()
  },

  methods: {

    // 查询行业列表
    getTreeListHandle() {
      getIndustryList().then(res => {
        if (res?.length) {
          this.treeList = [{
            industryCode: '0',
            industryName: '所属行业',
            childMenuEntities: res
          }]
        }
      }).catch(() => {})
    },

    // 获取详情
    getDetail() {
      const data = {
        id: this.id
      }
      getUserDetail(data).then(res => {
        const { industryCodes = [] } = res
        this.form = {
          industryCodes
        }
        this.defaultKeys = industryCodes
      }).catch(() => {
      })
    },

    handleCheckChange() {
      this.form.industryCodes = this.$refs.treeNode.getCheckedKeys()
    },

    // 取消
    close() {
      this.$emit('close')
    },

    // 确定
    confirm() {
      this.loading = true
      const data = {
        userId: this.id,
        ...this.form
      }
      addUserApprove(data).then(() => {
        this.loading = false
        this.$message.success('修改成功')
        this.$emit('confirm')
        this.close()
      }).catch(() => {
        this.loading = false
      })
    }
  }

}
</script>

<style lang="scss" scoped></style>
